package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SelfStudyRoomPojo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SchoolAreaMapper;
import com.ruoyi.system.mapper.SelfStudyRoomMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author czg
 * @date 2021/9/27 10:18 下午
 */
@Api(tags = "自习室预约服务")
@RestController
@RequestMapping("/api/selfStudyRoom")
public class SelfStudyRoomController {

    @Autowired
    SelfStudyRoomMapper selfStudyRoomMapper;

    @Autowired
    SchoolAreaMapper schoolAreaMapper;

    @PostMapping("/list")
    @ApiOperation("自习室列表")
    public AjaxResult<List<SelfStudyRoomPojo>> listSelfStudyRoom(@RequestBody SelfStudyRoomPojo schoolAreaPojo) {
        QueryWrapper<SelfStudyRoomPojo> query = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(schoolAreaPojo.getName())) {
            query.eq("name", schoolAreaPojo.getName());
        }
        if (StringUtils.isNotEmpty(schoolAreaPojo.getLocation())) {
            query.eq("location", schoolAreaPojo.getLocation());
        }
        if (StringUtils.isNotEmpty(schoolAreaPojo.getCampus())) {
            query.eq("campus", schoolAreaPojo.getCampus());
        }
        if (schoolAreaPojo.getStatus() != null) {
            query.eq("status", schoolAreaPojo.getStatus());
        }
        return AjaxResult.success(selfStudyRoomMapper.selectList(query));
    }

    @PostMapping("/add")
    @ApiOperation("新增自习室")
    public AjaxResult addSelfStudyRoom(@RequestBody SelfStudyRoomPojo schoolAreaPojo) {
        selfStudyRoomMapper.insert(schoolAreaPojo);
        return AjaxResult.success();
    }


    @PutMapping("/update")
    @ApiOperation("修改自习室")
    public AjaxResult updateSelfStudyRoom(@RequestBody SelfStudyRoomPojo schoolAreaPojo) {
        selfStudyRoomMapper.updateById(schoolAreaPojo);
        return AjaxResult.success();
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除自习室")
    public AjaxResult deleteSchoolArea(@PathVariable Integer id) {
        schoolAreaMapper.deleteById(id);
        return AjaxResult.success();
    }

    @GetMapping("/list/xuhui")
    @ApiOperation("徐汇校区可预约自习室")
    public AjaxResult<List<SelfStudyRoomPojo>> listStudyRoom1() {
        QueryWrapper<SelfStudyRoomPojo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("campus", "徐汇校区");
        return AjaxResult.success(selfStudyRoomMapper.selectList(queryWrapper));
    }

    @GetMapping("/list/fengxian")
    @ApiOperation("奉贤校区可预约自习室")
    public AjaxResult<List<SelfStudyRoomPojo>> listStudyRoom2() {
        QueryWrapper<SelfStudyRoomPojo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("campus", "奉贤校区");
        return AjaxResult.success(selfStudyRoomMapper.selectList(queryWrapper));
    }
}
